package com.liy.framework.web.spring.controller;

import com.liy.framework.common.annotation.MethodPermission;
import com.liy.framework.common.extvo.ResultVO;
import com.liy.framework.common.utils.Tools;
import com.liy.framework.common.vo.SupportVO;
import com.liy.framework.web.spring.service.IBaseService;
import java.io.Serializable;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Validated
public class BaseCrudController<Biz extends IBaseService, Entity extends SupportVO<String>> extends BaseController {
    protected static Logger log = LoggerFactory.getLogger(BaseCrudController.class);
    SpringValidatorAdapter a;
    @Autowired
    protected Biz baseService;

    public BaseCrudController() {}

    @RequestMapping(
            value = {"/add"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    @MethodPermission(
            codes = {"INSERT"},
            description = "新增"
    )
    public ResultVO add(@RequestBody @Valid Entity entity, BindingResult bindingResult) {
        try {
            if (Tools.StringIsNullOrSpace((String)entity.getId())) {
                entity.setId(Tools.getNewUUID());
            }

            this.baseService.add(entity);
            return new ResultVO(true, "新增成功", "200", entity);
        } catch (Exception var4) {
            log.error(var4.getMessage(), var4);
            return new ResultVO(false, "新增时发生错误:" + var4.getMessage(), "500");
        }
    }

    @RequestMapping(
            value = {"/update"},
            method = {RequestMethod.PUT}
    )
    @ResponseBody
    @MethodPermission(
            codes = {"UPDATE"},
            description = "修改"
    )
    public ResultVO update(@RequestBody Entity entity) {
        try {
            if (Tools.StringIsNullOrSpace((String)entity.getId())) {
                return new ResultVO(false, "请求参数不完整.");
            } else if (this.baseService.findById((Serializable)entity.getId()) == null) {
                return new ResultVO(false, "数据不存在或已被删除.");
            } else {
                this.baseService.update(entity);
                return new ResultVO(true, "修改成功", "200", entity);
            }
        } catch (Exception var3) {
            log.error(var3.getMessage(), var3);
            return new ResultVO(false, "修改时发生错误:" + var3.getMessage(), "500");
        }
    }

    @RequestMapping(
            value = {"/del/{id}"},
            method = {RequestMethod.DELETE}
    )
    @ResponseBody
    @MethodPermission(
            codes = {"DELETE"},
            description = "删除"
    )
    public ResultVO delete(@PathVariable String id) {
        try {
            Entity entity = (Entity) this.baseService.findById(id);
            if (entity == null) {
                return new ResultVO(false, "数据不存在或已被删除.");
            } else {
                this.baseService.delete(id);
                return new ResultVO(true, "删除成功");
            }
        } catch (Exception var3) {
            log.error(var3.getMessage(), var3);
            return new ResultVO(true, "删除时发生错误:" + var3.getMessage(), "500");
        }
    }
}
